<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <script src="/js/jquery-3.4.1.min.js"></script>
    <link rel="stylesheet" href="/css1/bootstrap.min.css">
    <script src="/js1/bootstrap.min.js"></script>
    <link rel="stylesheet" href="/css/style.css">


</head>
<body>
<!--<div class="container-fluid">-->
<div class="container">
    <div class="row">
        <div class="col">

        </div>
        <div class="col-9 border">
            <h4 class="font">学生表</h4>
            <hr>
            <div class="search">

<!--                <form class="row row-cols-lg-auto g-3 align-items-center" action="http://localhost:8080/student/list" method="get">-->
<!--                    <div class="col-12">-->
<!--                        <label class="visually-hidden" for="exampleInputName2">Name</label>-->
<!--                        <input type="text"  name="name" class="form-control" id="exampleInputName2" placeholder="请输入你要查询的名字">-->
<!--                    </div>-->

<!--                    <div class="col-12">-->
<!--                        <button type="submit"  class="btn btn-success" onclick="getList();">查询</button>-->
<!--                        <button type="submit"  class="btn btn-success" onclick="resetList();">刷新</button>-->
<!--                        <button type="button" class="btn btn-success" data-bs-toggle="modal" data-bs-target="#exampleModal" onclick="preAdd();">-->
<!--                            新增学生-->
<!--                        </button>-->
<!--                    </div>-->
<!--                </form>-->



                <div class="container">
                    <div class="row align-items-start">
                        <div class="col">
                            <label class="visually-hidden" for="exampleInputName2">Name</label>
                            <input type="text"  name="name" class="form-control" id="exampleInputName2" placeholder="请输入你要查询的名字">
                        </div>
                        <div class="col">
                            <button type="submit"  class="btn btn-success" onclick="getList();">查询</button>
                            <button type="submit"  class="btn btn-outline-success" onclick="resetList();">刷新</button>
                            <button type="button" class="btn btn-success" data-bs-toggle="modal" data-bs-target="#exampleModal" onclick="preAdd();">
                                新增学生
                            </button>
                        </div>
                    </div>
                </div>

            </div>
            <br>

            <table class="table table-success table-striped">
                <thead>
                <tr>
                    <th>#</th>
                    <th>学号</th>
                    <th>姓名</th>
                    <th>性别</th>
                    <th>成绩</th>
                    <th style="width:100px">操作</th>
                </tr>
                </thead>
                <tbody id="studentTb">
<!--                <tr data-th-each="student:${data}">-->
<!--                    <th scope="row" data-th-text="${student.getId()}" class="font">1</th>-->
<!--                    <td data-th-text="${student.getSno()}" class="font">Mark</td>-->
<!--                    <td data-th-text="${student.getSname()}" class="font">Mark</td>-->
<!--                    <td data-th-text="${student.getSex()}" class="font">Otto</td>-->
<!--                    <td data-th-text="${student.getScore()}" class="font">@mdo</td>-->
<!--                    <td class="font">-->
<!--                        <button type="button" class="btn btn-success">修改</button>-->
<!--                        <button type="button" class="btn btn-danger">删除</button>-->
<!--                    </td>-->
<!--                </tr>-->
                </tbody>
            </table>


            <nav class="modal-footer no-margin-top" aria-label="...">
                <ul class="pagination pull-right no-margin pagination">
                    <li class="page-item disabled">
                        <a th:href="'/list.html?pageNum=0'" class="page-link">首页</a>
                    </li>

                    <!-- 上一页 -->
                    <li th:if="${users.hasPrevious()}" class="page-item">
                        <a class="page-link" th:href="'/page.html?pageNum=' + ${users.previousPageable().getPageNumber()}" th:text="上一页"></a>
                    </li>

                    <!-- 中间页 -->
                    <li class="page-item" th:each="pageNum:${#numbers.sequence(0, users.getTotalPages() - 1)}">
                        <a th:href="'/page.html?pageNum=' + ${pageNum}" th:text="${pageNum + 1}" th:if="${pageNum ne users.pageable.getPageNumber()}" class="page-link"></a>
                        <a class="page-link" th:href="'page.html?pageNum=' + ${pageNum}" th:text="${pageNum + 1}" th:if="${pageNum eq users.pageable.getPageNumber()}" th:style="'font-weight:bold;background: #6faed9;'"></a>
                    </li>

                    <!-- 下一页 -->
                    <li th:if="${users.hasNext()}" class="page-item">
                        <a class="page-link" th:href="'/page.html?pageNum=' + ${users.nextPageable().getPageNumber()}" th:text="下一页"></a>
                    </li>

                    <!-- 尾页 -->
                    <li class="page-item disabled">
                        <a th:href="'/page.html?pageNum=' + ${users.getTotalPages() - 1}" class="page-link">尾页</a>
                    </li>

                </ul>
            </nav>
<!--                <div th:if="${message} != null">-->
<!--                    <div class="alert alert-danger alert-dismissible fade show" role="alert" >-->
<!--                        <strong>查无此人!</strong>-->
<!--                        <button type="button" class="btn-close" data-bs-dismiss="alert" aria-label="Close"></button>-->
<!--                    </div>-->
<!--                </div>-->
        </div>
    </div>
</div>



<!-- Modal -->
<div class="modal fade" id="exampleModal" tabindex="-1" aria-labelledby="exampleModalLabel" aria-hidden="true">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header">
                <h5 class="modal-title" id="exampleModalLabel">学生信息</h5>
                <button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
            </div>
            <div class="modal-body">

                <form id="formStudent">

                    <input hidden="hidden" id="id" name="id">
                    <div class="form-group">
                        <label for="sno">学号</label>
                        <input type="text" class="form-control" id="sno" aria-describedby="emailHelp" placeholder="Enter sno" name="sno">
                    </div>
                    <div class="form-group">
                        <label for="sname">姓名</label>
                        <input type="text" class="form-control" id="sname" aria-describedby="emailHelp" placeholder="Enter sname" name="sname">
                    </div>

                    <div class="form-group">
                        <label for="sex">性别</label>
                        <select class="form-control" id="sex" name="sex">
                            <option value="男">男</option>
                            <option value="女">女</option>
                        </select>
                    </div>
<!--                    <div class="form-group">-->
<!--                        <label for="age">年龄</label>-->
<!--                        <input type="text" class="form-control" id="age" aria-describedby="emailHelp" placeholder="Enter age" name="age">-->
<!--                    </div>-->
                    <div class="form-group">
                        <label for="score">成绩</label>
                        <input type="text" class="form-control" id="score" aria-describedby="emailHelp" placeholder="Enter score" name="score">
                    </div>

                </form>
<!--                表单结束-->

            </div>
            <div class="modal-footer">
<!--                <button type="button" class="btn btn-primary" onclick="addStudent()">保存学生</button>-->
                <button type="button" class="btn btn-success" onclick="saveStudent();">保存学生</button>
            </div>
        </div>
    </div>
</div>



</div>
    <script src="/js/jquery-3.4.1.min.js"></script>
<script>
    /**
     * 刷新数据
     */
    function resetList(){
        loadTable();
        $("#exampleInputName2").val(" ")
    }

    /**
     * 根据姓名查询学生
     */
    function getList(){
        var name = $("#exampleInputName2").val().toString();
        if(name == null || name == ""){
            alert("请输入要查询的姓名");
        } else{

            console.log(name)
            $.ajax({
                url: "/webapi/student/getList/" + name,
                method: "get"
            }).done(function (rs){
                    let len = rs.length;
                    if(len == 0){
                        alert("查无此人");
                    } else{
                        let html = "";
                        for(let i = 0; i < len; i++){
                            let item = rs[i];
                            html+="<tr>"
                                +"<td>"+item.id+"</td>"
                                +"<td>"+item.sno+"</td>"
                                +"<td>"+item.sname+"</td>"
                                +"<td>"+item.sex+"</td>"
                                +"<td>"+item.score+"</td>"
                                +"<td> "
                                +"<a href='#' style='text-decoration: none;color: #0c4128' onclick='editStudent("+item.id+");'>编辑</a>   "
                                +"<a href='#' style='text-decoration: none;color: #0c4128' onclick='deleteStudent("+item.id+")'>删除</a>"
                                +"</td>"
                                +"</tr>"
                            $("#studentTb").html(html)
                        }
                    }



            })

        }


    }

    /**
     * 与表进行判断查询
     */
    // function getList(){
    //     var name = $("#exampleInputName2").val().toString();
    //     // alert(name)
    //     console.log(name)
    //     $.ajax({
    //         url: "/webapi/student/list",
    //         method:"get"
    //     }).done(function (rs){
    //         // loadTable();
    //         // console.log(name);
    //         let len = rs.length;
    //         let html = "";
    //         for(let i = 0; i < len; i++){
    //             let item = rs[i];
    //            if(item.sname == name){
    //                html+="<tr>"
    //                    +"<td>"+item.id+"</td>"
    //                    +"<td>"+item.sno+"</td>"
    //                    +"<td>"+item.sname+"</td>"
    //                    +"<td>"+item.sex+"</td>"
    //                    +"<td>"+item.score+"</td>"
    //                    +"<td> "
    //                    +"<a href='#' onclick='editStudent("+item.id+");'>编辑</a>   "
    //                    +"<a href='#' onclick='deleteStudent("+item.id+")'>删除</a>" +
    //                    "</td>"
    //                    +"</tr>"
    //                $("#studentTb").html(html)
    //            }else if(name == " " || name == null){
    //                loadTable()
    //            }
    //         }
    //     })
    //
    // }

    /**
     * 根据id删除学生
     * @param id
     */
    function deleteStudent(id){
        if(confirm("您是否真的要删除吗？")){
            $.ajax({
                url:"/webapi/student/delete/"+id,
                method:"delete"

            }).done(function (){
                loadTable();
            })
        }
    }

    /**
     * 根据id判断是新增还是更新，判断之后做新增和更新操作
     */
    function saveStudent(){
        var data = $('#formStudent').serialize();

        var id = $("#id").val();

        if(id < 1) {

            //是新增
            $.ajax ({
                url:"/webapi/student/insert",
                method:"post",
                data:data
            }).done(function () {

                loadTable();

                $('#exampleModal').modal('hide');
            });
        }
        else {

            //是更新
            $.ajax ({

                url:"/webapi/student/update",
                method:"put",

                data:data

            }).done(function () {

                loadTable();

                $('#exampleModal').modal('hide');
            });
        }
    }

    /**
     * 点击新增时给id赋值，以便判断做新增操作
     */
    function preAdd(){
        $("#id").val(0);//提示新增
        $("#sno").val("");
        $("#sname").val("");
        $("#sex").val("");
        $("#score").val("");
    }

    function addStudent(){
        var data = $("#formStudent").serialize();
        $.ajax({
            url: "/webapi/student/insert",
            method:"post",
            data:data
        }).done(function (){
            loadTable();
            $('#exampleModal').modal('hide')
        })
    }

    /**
     * 根据id编辑学生，同时读取学生信息
     * @param id
     */
    function editStudent(id){
        $('#exampleModal').modal('show')
         $.ajax({
             url:'/webapi/student/get/'+id
         }).done(function (rs){

             $("#id").val(rs.id);
             $("#sno").val(rs.sno);
             $("#sname").val(rs.sname);
             $("#sex").val(rs.sex);
             $("#score").val(rs.score);

         })
    }

    /**
     * 显示整个学生列表
     */
    function loadTable(){
        $.ajax({
            url:"/webapi/student/list"
        }).done(function (rs){
            let len = rs.length;
            let html = "";
            for(let i = 0; i < len; i++){
                let item = rs[i];
                html+="<tr>"
                    +"<td>"+item.id+"</td>"
                    +"<td>"+item.sno+"</td>"
                    +"<td>"+item.sname+"</td>"
                    +"<td>"+item.sex+"</td>"
                    +"<td>"+item.score+"</td>"
                    +"<td> "
                    +"<a href='#' onclick='editStudent("+item.id+");'>编辑</a>   "
                    +"<a href='#' onclick='deleteStudent("+item.id+")'>删除</a>" +
                    "</td>"
                    +"</tr>"
            }
            $("#studentTb").html(html)
        })
    }

   $(function(){
        loadTable()
    })
</script>

</body>
</html>